import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:xmshop/app/models/message.dart';
import 'package:xmshop/app/services/screebAdapter.dart';

import '../controllers/pass_login_controller.dart';
import '../../../../widget/logo.dart';
import '../../../../widget/passButton.dart';
import '../../../../widget/passTextField.dart';
import '../../../../widget/userAgreement.dart';

class PassLoginView extends GetView<PassLoginController> {
  const PassLoginView({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0,
        actions: [TextButton(onPressed: () {}, child: const Text("帮助"))],
      ),
      body: ListView(
        padding: EdgeInsets.all(ScreenAdapter.width(40)),
        children: [
          const Logo(),
          PassTextFiled(
              controller: controller.telController, hintText: "输入手机号"),
          PassTextFiled(
            hintText: "密码",
            isPassWord: true,
            controller: controller.passController,
          ),
          const UserAgreement(),
          PassButton(
              text: "登录",
              onPressed: () async {
                // 检测手机号是否合法
                if (!GetUtils.isPhoneNumber(controller.telController.text) ||
                    controller.telController.text.length != 11) {
                  Get.snackbar('提示信息', "手机号输入有误");
                } else if (controller.passController.text.length < 6) {
                  Get.snackbar("提示信息", "密码长度不能小于6位");
                } else {
                  MessageModel res = await controller.doLogin();
                  if (res.success) {
                    // 登录成功，回到根路径
                    Get.back();
                  } else {
                    Get.snackbar('提示信息', res.message);
                  }
                }
              }),
          Container(
            child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  TextButton(onPressed: () {}, child: const Text('忘记密码')),
                  TextButton(
                      onPressed: () {
                        Get.toNamed('/code-login-step-one');
                      },
                      child: const Text('验证码登录')),
                ]),
          )
        ],
      ),
    );
  }
}
